{% load static %}
{% block css %}
 <style>

/* 导入字体 */
    @import url('https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&display=swap');

/* 新增返回按钮样式 */
        .back-to-home {
            position: fixed;
            bottom: 20px;
            right: 20px;
            z-index: 1000;
            padding: 12px 20px;
            background: linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%);
            color: white;
            border: none;
            border-radius: 30px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: 500;
            box-shadow: 0 4px 15px rgba(255, 154, 158, 0.3);
            display: flex;
            align-items: center;
            text-decoration: none;
        }

        .back-to-home:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(255, 154, 158, 0.4);
        }

        .back-to-home:active {
            transform: translateY(0);
        }

        .back-to-home i {
            margin-right: 8px;
        }

        @media (max-width: 600px) {
            .back-to-home {
                bottom: 70px;
                right: 15px;
                padding: 10px 15px;
                font-size: 0.9rem;
            }
        }
        .chat-box {
            overscroll-behavior: contain;
            scroll-behavior: smooth;
        }
 /* 新增导航栏高度对应的顶部边距 */
        .container {
            margin-top: 9rem; /* 根据实际导航栏高度调整这个值 */
            padding: 0;
        }

        .header {
            height: 1.5rem; /* 如果尚未定义请添加这一行 */
            position: fixed;
            top: 0;
            width: 100%;
            z-index: 1000; /* 保持导航栏在最上层 */
        }

/* 主布局调整 */
    html, body {
        height: 100%;
    }

    body {
        display: flex;
        flex-direction: column;
        background: linear-gradient(135deg, #fff5f5 0%, #ffebf3 50%, #fef5ff 100%);
        animation: gradientBG 15s ease infinite;
        background-size: 200% 200%;
        padding-top: 60px; /* 导航栏高度 */
        padding-bottom: 60px; /* 页脚高度 */
    }

    /* 主内容区域 */
    .main-content {
        flex: 1;
        display: flex;
        flex-direction: column;
        overflow: auto;
        padding: 20px 0;
    }

    .container {
        max-width: 800px;
        margin: 0 auto 20px;
        background: rgba(255, 255, 255, 0.9);
        border-radius: 20px;
        box-shadow: 0 10px 30px rgba(255, 182, 193, 0.3);
        flex-grow: 1;
        display: flex;
        flex-direction: column;
        width: 95%;
        overflow: hidden;
        position: relative;
        backdrop-filter: blur(5px);
        border: 1px solid rgba(255, 255, 255, 0.5);
    }

        /* 聊天框高度调整 */
        .chat-box {
            flex-grow: 1;
            padding: 20px;
            overflow-y: auto;
            max-height: calc(100vh - 280px); /* 动态计算高度 */
            background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><path fill="%23ffebf3" fill-opacity="0.2" d="M30,50 Q50,30 70,50 Q50,70 30,50"/></svg>');
        }

        .header {
        background: linear-gradient(to right, #ffb6c1, #ffc0cb);
        color: white;
        padding: 15px 20px;
        text-align: center;
        font-weight: 500;
        font-size: 1.2rem;
        box-shadow: 0 2px 10px rgba(255, 182, 193, 0.3);
        position: relative;
        z-index: 1;
    }

        /* 输入区域固定 */
        .input-area {
            position: sticky;
            bottom: 0;
            background: rgba(255, 255, 255, 0.95);
            z-index: 2;
        }

        /* 响应式调整 */
        @media (max-width: 600px) {
            body {
                padding-top: 50px;
                padding-bottom: 50px;
            }

            .chat-box {
                max-height: calc(100vh - 240px);
            }
        }
        .header::after {
            content: "";
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            height: 3px;
            background: linear-gradient(to right, transparent, #fff, transparent);
        }

        .chat-box {
            flex-grow: 1;
            padding: 20px;
            overflow-y: auto;
            height: 60vh;
            background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><path fill="%23ffebf3" fill-opacity="0.2" d="M30,50 Q50,30 70,50 Q50,70 30,50"/></svg>');
        }

        .message {
            margin: 15px 0;
            max-width: 80%;
            padding: 15px 20px;
            border-radius: 20px;
            animation: fadeIn 0.5s ease-out;
            position: relative;
            line-height: 1.5;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
            word-break: break-word;
        }

        .user {
            background: linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%);
            color: white;
            margin-left: auto;
            border-top-right-radius: 5px;
        }

        .assistant {
            background: linear-gradient(135deg, #f5f7fa 0%, #fff 100%);
            margin-right: auto;
            border-top-left-radius: 5px;
            border: 1px solid #ffe6eb;
        }

        .message::after {
            content: "";
            position: absolute;
            bottom: -10px;
            width: 20px;
            height: 20px;
            background: inherit;
            clip-path: polygon(0 0, 100% 0, 100% 100%);
            transform: rotate(45deg);
        }

        .user::after {
            right: 10px;
            background: linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%);
        }

        .assistant::after {
            left: 10px;
            background: linear-gradient(135deg, #f5f7fa 0%, #fff 100%);
        }

        .input-area {
            padding: 20px;
            border-top: 1px solid #ffe6eb;
            display: flex;
            gap: 10px;
            background: rgba(255, 255, 255, 0.8);
            position: relative;
        }

        input {
            flex-grow: 1;
            padding: 15px 20px;
            border: 1px solid #ffd6e0;
            border-radius: 30px;
            outline: none;
            font-size: 1rem;
            background: rgba(255, 255, 255, 0.9);
            transition: all 0.3s ease;
            box-shadow: 0 2px 10px rgba(255, 182, 193, 0.1);
        }

        input:focus {
            border-color: #ffb6c1;
            box-shadow: 0 0 0 3px rgba(255, 182, 193, 0.2);
        }

        button {
            padding: 15px 25px;
            background: linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%);
            color: white;
            border: none;
            border-radius: 30px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: 500;
            box-shadow: 0 4px 15px rgba(255, 154, 158, 0.3);
            display: flex;
            align-items: center;
            justify-content: center;
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(255, 154, 158, 0.4);
        }

        button:active {
            transform: translateY(0);
        }

        .typing-indicator {
            display: flex;
            padding: 10px 15px;
            background: #f5f5f5;
            border-radius: 20px;
            margin-right: auto;
            width: fit-content;
            align-items: center;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
            border: 1px solid #ffe6eb;
        }

        .typing-dot {
            width: 8px;
            height: 8px;
            margin: 0 2px;
            background: #ffb6c1;
            border-radius: 50%;
            animation: typingAnimation 1.4s infinite ease-in-out;
        }

        .typing-dot:nth-child(1) {
            animation-delay: 0s;
        }

        .typing-dot:nth-child(2) {
            animation-delay: 0.2s;
        }

        .typing-dot:nth-child(3) {
            animation-delay: 0.4s;
        }

        @keyframes typingAnimation {
            0%, 60%, 100% {
                transform: translateY(0);
            }
            30% {
                transform: translateY(-5px);
            }
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        @keyframes gradientBG {
            0% {
                background-position: 0% 50%;
            }
            50% {
                background-position: 100% 50%;
            }
            100% {
                background-position: 0% 50%;
            }
        }

        .floating-hearts {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            pointer-events: none;
            z-index: 0;
            overflow: hidden;
        }

        .heart {
            position: absolute;
            opacity: 0;
            animation: floatUp 10s linear infinite;
            color: rgba(255, 182, 193, 0.5);
        }

        @keyframes floatUp {
            0% {
                transform: translateY(100vh) rotate(0deg);
                opacity: 0;
            }
            10% {
                opacity: 0.5;
            }
            90% {
                opacity: 0.5;
            }
            100% {
                transform: translateY(-100px) rotate(360deg);
                opacity: 0;
            }
        }

        .welcome-animation {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 2rem;
            color: #ff9a9e;
            opacity: 0;
            animation: welcomeFade 2s ease-out forwards;
            z-index: 10;
            text-shadow: 0 2px 10px rgba(255, 154, 158, 0.3);
        }

        @keyframes welcomeFade {
            0% {
                opacity: 0;
                transform: translate(-50%, -50%) scale(0.5);
            }
            50% {
                opacity: 1;
                transform: translate(-50%, -50%) scale(1.1);
            }
            100% {
                opacity: 0;
                transform: translate(-50%, -50%) scale(0.8);
            }
        }

        @media (max-width: 600px) {
            .message {
                max-width: 90%;
                padding: 12px 15px;
            }

            .input-area {
                padding: 15px;
                flex-direction: column;
            }

            button {
                width: 100%;
            }
        }


    </style>
{% endblock %}

{% block body %}


<div class="main-content">
    <div class="floating-hearts" id="floatingHearts"></div>
    <!-- 在main-content闭合标签前添加返回按钮 -->
    <a href="/home/" class="back-to-home">
        <i class="fas fa-home"></i> 返回主页
    </a>
    <div class="container">
        <div class="header">
            <span> 心灵治愈助手 </span>
        </div>

        <div class="chat-box" id="chatBox">
            <div class="message assistant">你好呀！我是你的心灵治愈助手<br><br>在这里，你可以畅所欲言，我会用温暖的粉色能量为你带来安慰和支持。今天有什么想和我分享的吗？</div>
        </div>

        <div class="input-area">
            <input type="text" id="userInput" placeholder="输入你的心事或问题..." @keyup.enter="sendMessage">
            <button onclick="sendMessage()">
                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="margin-right: 8px;">
                    <line x1="22" y1="2" x2="11" y2="13"></line>
                    <polygon points="22 2 15 22 11 13 2 9 22 2"></polygon>
                </svg>
                发送
            </button>
        </div>
    </div>
</div>
    {% block script %}
        <script>
        function createHearts() {
                const container = document.getElementById('floatingHearts');
                const heartIcons = ['❤️', '🌸', '💮', '🏵️', '💗', '💖', '💓', '💞'];

                for (let i = 0; i < 15; i++) {
                    const heart = document.createElement('div');
                    heart.className = 'heart';
                    heart.innerHTML = heartIcons[Math.floor(Math.random() * heartIcons.length)];
                    heart.style.left = Math.random() * 100 + 'vw';
                    heart.style.fontSize = (Math.random() * 20 + 10) + 'px';
                    heart.style.animationDuration = (Math.random() * 10 + 5) + 's';
                    heart.style.animationDelay = Math.random() * 5 + 's';
                    container.appendChild(heart);
                }
            }

            // 显示欢迎动画
            function showWelcomeAnimation() {
                const welcome = document.createElement('div');
                welcome.className = 'welcome-animation';
                welcome.textContent = '愿你在这里找到平静与治愈';
                document.body.appendChild(welcome);

                setTimeout(() => {
                    welcome.remove();
                }, 2000);
            }

            // 添加消息到聊天框
            function addMessage(role, content) {
                const chatBox = document.getElementById('chatBox');

                // 如果是助手消息，先显示"正在输入"指示器
                if (role === 'assistant') {
                    const typingIndicator = document.createElement('div');
                    typingIndicator.className = 'typing-indicator';
                    typingIndicator.innerHTML = `
                            <div class="typing-dot"></div>
                            <div class="typing-dot"></div>
                            <div class="typing-dot"></div>
                        `;
                    chatBox.appendChild(typingIndicator);
                    chatBox.scrollTop = chatBox.scrollHeight;

                    // 模拟打字效果
                    setTimeout(() => {
                        typingIndicator.remove();
                        const messageDiv = document.createElement('div');
                        messageDiv.className = `message ${role}`;
                        messageDiv.innerHTML = content;
                        chatBox.appendChild(messageDiv);
                        chatBox.scrollTop = chatBox.scrollHeight;

                        // 添加爱心特效
                        if (role === 'assistant') {
                            addHeartEffect(messageDiv);
                        }
                    }, 1000 + content.length * 30); // 根据消息长度调整延迟
                } else {
                    const messageDiv = document.createElement('div');
                    messageDiv.className = `message ${role}`;
                    messageDiv.textContent = content;
                    chatBox.appendChild(messageDiv);
                    chatBox.scrollTop = chatBox.scrollHeight;
                }
            }

            // 添加爱心特效
            function addHeartEffect(element) {
                const rect = element.getBoundingClientRect();
                for (let i = 0; i < 3; i++) {
                    const heart = document.createElement('div');
                    heart.style.position = 'fixed';
                    heart.style.left = (rect.left + Math.random() * rect.width) + 'px';
                    heart.style.top = (rect.top + Math.random() * rect.height) + 'px';
                    heart.style.fontSize = (Math.random() * 20 + 10) + 'px';
                    heart.style.opacity = '0';
                    heart.style.transition = 'all 1s ease-out';
                    heart.innerHTML = '🌸';
                    document.body.appendChild(heart);

                    setTimeout(() => {
                        heart.style.opacity = '1';
                        heart.style.transform = `translate(${Math.random() * 40 - 20}px, ${-Math.random() * 50 - 30}px) rotate(${Math.random() * 360}deg)`;
                    }, 10);

                    setTimeout(() => {
                        heart.style.opacity = '0';
                        setTimeout(() => heart.remove(), 1000);
                    }, 1000);
                }
            }

            // 发送消息
            async function sendMessage() {
                const input = document.getElementById('userInput');
                const message = input.value.trim();

                if (!message) return;

                addMessage('user', message);
                input.value = '';

                try {
                    const response = await fetch('/aitalk/', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify({message: message})
                    });

                    if (!response.ok) throw new Error('请求失败');

                    const data = await response.json();
                    if (data.response) {
                        addMessage('assistant', data.response);
                    } else {
                        throw new Error('无有效响应');
                    }
                } catch (error) {
                    addMessage('assistant', '🌸 抱歉，我暂时有点小困惑。能请你再说一次吗？');
                    console.error('Error:', error);
                }
            }

            // 初始化
            document.addEventListener('DOMContentLoaded', () => {
                createHearts();
                showWelcomeAnimation();

                // 绑定回车键事件
                document.getElementById('userInput').addEventListener('keypress', (e) => {
                    if (e.key === 'Enter') sendMessage();
                });
            });
        </script>
     {% endblock %}
{% endblock %}